package ViewPagerLib;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import TruongNhaLib.MarkAdapter;
import TruongNhaLib.MarkModel;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

public final class TestFragment extends Fragment {
    private static final String KEY_CONTENT = "TestFragment:Content";
    private static int itemListPosition = 0;
    private int startPosition;
    private int finishPosition;
    private String mContent = "???";
    private ArrayList<HashMap<String, String>> classList = new ArrayList<HashMap<String, String>>();
    private List<MarkModel> list = new ArrayList<MarkModel>();
    
    //Default constructor
    public TestFragment(){
    	super();
    }//end
    
    /*
     * TestFragment constructor.
     */
    public TestFragment(List<MarkModel> list, int startPosition, int endPosition ){
    	this.list = list;
    	this.startPosition = startPosition;
    	this.finishPosition = endPosition;
    }//end
    /*
     * TestFragment constructor.
     */
    public TestFragment(ArrayList<HashMap<String, String>> classList, int startPosition, int endPosition ){
    	this.classList = classList;
    	this.startPosition = startPosition;
    	this.finishPosition = endPosition;
    }//end
    
    /**
     * 
     * @param content: The title string
     * @return fragment
     */
    public static TestFragment newInstance(String content) {
        TestFragment fragment = new TestFragment();

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 20; i++) {
            builder.append(content).append(" ");
        }
        builder.deleteCharAt(builder.length() - 1);
        fragment.mContent = builder.toString();

        return fragment;
    }
    /**
     * 
     * @param content: The title string
     * @param classListModel: The markModel
     * @return fragment
     */
    public static TestFragment newInstaceModel(String content, ArrayList<HashMap<String, String>> classListModel){
    	TestFragment fragment = new TestFragment();
    	fragment.classList = classListModel;
    	
    	return fragment;
    }//end. 
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
            mContent = savedInstanceState.getString(KEY_CONTENT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    	
    	Log.d("postition TestFragment: ", startPosition +"");
    	//Create a linearlayout as a container:
    	LinearLayout layout = new LinearLayout(getActivity());
        layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        layout.setGravity(Gravity.CENTER);
        Log.d("lv cin", "01");
       
        ListView v = new ListView( getActivity() );
        ArrayAdapter<MarkModel> adapter = new MarkAdapter( getActivity() , list, startPosition, finishPosition);
        v.setAdapter(adapter);
        
        //Set itemList position and listen scroll to change itemList position for other lists.	
        v.setSelection(itemListPosition);
		Log.d("setSelection", "itemPosition = " + itemListPosition);

        v.setOnScrollListener(new OnScrollListener() {
			
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
//				Log.d("onScrollStateChanged", "change");
			}//end
			
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				itemListPosition = firstVisibleItem;
//				Log.d("onScroll", "change" + firstVisibleItem + "|" +visibleItemCount+"|" + totalItemCount);
//				Log.d("onScroll", "itemListPosition were changed to " + itemListPosition);
			}//end
		});
    	Log.d("lv-cout", "03");

    	//layout.addView(v);
    	return v;
        //return layout;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_CONTENT, mContent);
    }
}
